﻿using System;
using System.Windows.Forms;
using BikeManageBusiness.logical;
using Data.Common;
using Data.Enum;
using Data.Model;
using DataAccess.AccountManage;
using DataAccess.BikeManage.logical;
using Ninject;
using WinForm.Helper;

namespace WinForm.UserManage
{
    /// <summary>
    /// 已经不使用，使用ModifyPasswordFrm取代
    /// </summary>
    public partial class FrmModifyPassword : Form
    {
        private static readonly IKernel Kernel = new StandardKernel(new DbModule());
        private readonly IAccount _account = Kernel.Get<IAccount>();

        public FrmModifyPassword()
        {
            InitializeComponent();
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            try
            {
                var zq58Logical = new Zq58Logical();
                ResultInfo<CpuAccount> cpuaccount = zq58Logical.ReadAccountInfo();
                if (cpuaccount.ResultNumber == ResultEnum.Success)
                {
                    txtCpuCardNo.Text = cpuaccount.Obj.CpuCardId;
                }
            }
            catch (Exception)
            {
            }
            tabItem2.Visible = false;
            tabItem1.Visible = true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string errorMessage = string.Empty;
            try
            {
                GC.Collect();
                var dkqa16DLogical = new Dkqa16dLogical();
                ResultInfo<IDCardData> idinfo = dkqa16DLogical.GetIdCardData();
                if (idinfo.ResultNumber == ResultEnum.Success)
                {
                    txtIdCardNo.Text = idinfo.Obj.IDCardNo;
                }
                if (string.IsNullOrEmpty(txtIdCardNo.Text))
                {
                    errorMessage = errorMessage + "请输入身份证号码,或者将身份证放在读卡器上";
                }else
                {
                    if (string.IsNullOrEmpty(txtNewPassword.Text) || string.IsNullOrEmpty(txtNewPassword2.Text) ||
                        txtNewPassword.Text != txtNewPassword2.Text)
                    {
                        errorMessage = errorMessage + "输入的密码为空或者两次密码不一致";
                    }
                    else
                    {
                        var account = new ResultInfo<jsh_account> { ResultNumber = ResultEnum.Error };
                        //if (string.IsNullOrEmpty(txtCpuCardNo.Text))
                        //{
                        //    if (string.IsNullOrEmpty(txtIdCardNo.Text))
                        //    {
                        //        errorMessage = errorMessage + "请输入身份证号码,或者将身份证放在读卡器上";
                        //    }
                        //    else
                        //    {
                                account = _account.GetAccountWithIdCard(txtIdCardNo.Text);
                        //    }
                        //}
                        //else
                        //{
                        //    account = _account.GetAccount(txtCpuCardNo.Text);
                        //}
                        if (account.ResultNumber == ResultEnum.Success)
                        {
                            ResultInfo<bool> uppassword = _account.UpdateAccountPassword(account.Obj.id, txtNewPassword.Text);
                            if (uppassword.ResultNumber == ResultEnum.Success)
                            {
                                MyMessageBox.ShowMessageBoxInfo("修改密码成功！");
                                Close();
                                Dispose();
                            }
                            else
                            {
                                errorMessage = errorMessage + uppassword.Describe;
                            }
                        }
                        else
                        {
                            errorMessage = errorMessage + account.Describe;
                        }
                    }
                }
            }
            catch (Exception e1)
            {
                errorMessage = errorMessage + e1.Message;
            }
            GC.Collect();
            if (!string.IsNullOrEmpty(errorMessage))
            {
                MyMessageBox.ShowMessageBoxError(errorMessage);
            }
        }
    }
}